/**
 *
 */
package com.zhuoyue.elmt.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eit.eitsite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.elmt.entity.GgSydw;
import com.zhuoyue.elmt.service.GgSydwService;

import java.util.List;

/**
 * 使用单位信息Controller
 * @version 2016-12-18
 */
@Controller
@RequestMapping(value = "${adminPath}/elmt/ggSydw")
public class GgSydwController extends BaseController {

	@Autowired
	private GgSydwService ggSydwService;
	
	@ModelAttribute
	public GgSydw get(@RequestParam(required=false) String id) {
		GgSydw entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = ggSydwService.get(id);
		}
		if (entity == null){
			entity = new GgSydw();
		}
		return entity;
	}
	
	@RequiresPermissions("elmt:ggSydw:view")
	@RequestMapping(value = {"list", ""})
	public String list(String parentId,GgSydw ggSydw, HttpServletRequest request, HttpServletResponse response, Model model) {
		if(!UserUtils.getUser().isAdmin()) {
			ggSydw.setWbdwdm(UserUtils.getUser().getCompany().getCode());
		}
		if(StringUtils.isNotEmpty(parentId)){
			model.addAttribute("parentId", parentId);
		}
		Page<GgSydw> page = ggSydwService.findPage(new Page<GgSydw>(request, response), ggSydw); 
		model.addAttribute("page", page);
		return "zhuoyue/elmt/ggSydwList";
	}

	@RequiresPermissions("elmt:ggSydw:view")
	@RequestMapping(value = "form")
	public String form(GgSydw ggSydw, Model model) {
		model.addAttribute("ggSydw", ggSydw);
		return "zhuoyue/elmt/ggSydwForm";
	}

	@RequiresPermissions("elmt:ggSydw:edit")
	@RequestMapping(value = "save")
	public String save(GgSydw ggSydw, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, ggSydw)){
			return form(ggSydw, model);
		}
		ggSydw.setWbdwdm(UserUtils.getUser().getCompany().getCode());
		ggSydw.setWbdwmc(UserUtils.getUser().getCompany().getName());
		// 如果已经有了使用单位代码
		if(StringUtils.isEmpty(ggSydw.getId())) {
			List<GgSydw> existsSydw = ggSydwService.verifyWhetherAdd(ggSydw);
			if (existsSydw.size() > 0) {
				model.addAttribute("message", "使用单位已存在");
				return form(ggSydw, model);
			}
		}
		//ggSydw.setId(ggSydw.getZzjqdm());
		ggSydwService.save(ggSydw);
		addMessage(redirectAttributes, "保存使用单位信息成功");
		return "redirect:"+Global.getAdminPath()+"/elmt/ggSydw/?repage";
	}
	
	@RequiresPermissions("elmt:ggSydw:edit")
	@RequestMapping(value = "delete")
	public String delete(GgSydw ggSydw, RedirectAttributes redirectAttributes) {
		ggSydwService.delete(ggSydw);
		addMessage(redirectAttributes, "删除使用单位信息成功");
		return "redirect:"+Global.getAdminPath()+"/elmt/ggSydw/?repage";
	}

	@RequiresPermissions("elmt:ggSydw:edit")
	@RequestMapping(value = "verifyWhetherAdd")
	@ResponseBody
	public String verifyWhetherAdd(GgSydw ggSydw){
		ggSydw.setWbdwdm(UserUtils.getUser().getCompany().getCode());
		List<GgSydw> sydwlist= ggSydwService.verifyWhetherAdd(ggSydw);
		String successful = "";
		if(sydwlist.size() > 0){
			successful = "0";
		} else {
			successful = "1";
		}
		return successful;
	}
}